package com.gbq.boot.web.controller;


import com.gbq.boot.web.bean.Article;
import com.gbq.boot.web.bean.vo.TimeLineVo;
import com.gbq.boot.web.service.ArticleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gqb.boot.utils.PageBounds;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * @author aqian666
 */
@RestController
@RequestMapping("article")
public class ArticleController {

    @Resource
    private ArticleService articleService;

    /**
     * 搜索标题，描述，内容
     * @param keywords
     * @param currentPage  当前页
     * @param pageSize   分页大小
     * @return result
     */
    @GetMapping("/search")
    public HashMap<String, Object> search(String keywords,Integer currentPage,Integer pageSize){
        HashMap<String,Object> result = new HashMap<>();
        List<Article> articleList = articleService.getArticleList(keywords,currentPage,pageSize);
        PageBounds<Article> pageBounds = new PageBounds(currentPage,articleList.size(),pageSize);
        pageBounds.setPageList(articleList);
        result.put("data",pageBounds);
        return result;
    }


    /**
     * 发表文章
     */
    @PostMapping("/save")
    public HashMap<String, Object>  saveArticle(@RequestBody Article article){
        HashMap<String,Object> result = new HashMap<>();
        articleService.updateArticle(article);
        return result;
    }

    /**
     * 修改文章
     */
    @PutMapping("/update")
    public HashMap<String, Object>  updateArticle(@RequestBody Article article){
        HashMap<String,Object> result = new HashMap<>();
        articleService.updateArticle(article);
        return result;
    }

    /**
     * 删除文章
     */
    @DeleteMapping("/delete")
    public HashMap<String, Object>  deleteArticle(Integer[] ids){
        HashMap<String,Object> result = new HashMap<>();
        articleService.deleteArticle(Arrays.asList(ids));
        return result;
    }

    /**
     * 分页查询
     */
    @GetMapping("/list")
    public HashMap<String, Object> getArticlePage(Article article,Integer  pageSize,Integer currentPage){
        HashMap<String,Object> result = new HashMap<>();
        if (pageSize != null && currentPage != null){
            PageHelper.startPage(currentPage, pageSize);
            List<Article>  articleList= articleService.getArticleByPage(article);
            PageInfo<Article> pageInfo = new PageInfo<>(articleList);
            result.put("data",pageInfo);
        }else {
            List<Article>  articleList= articleService.getArticleByPage(article);
            result.put("data",articleList);
        }
        return result;
    }


    /**
     * 根据id查询文章具体内容
     * @param id 文章id
     * @return result
     */
    @GetMapping(value = "/{id}")
    public HashMap<String, Object> searchById(@PathVariable(value = "id") int id){
        HashMap<String,Object> result = new HashMap<>();
        // 对所有索引进行搜索
        Article article = articleService.searchById(id);
        result.put("data",article);
        return result;
    }

    /**
     * 查询标签墙
     * @return 标签内容
     */
    @GetMapping(value = "/searchTag")
    public HashMap<String, Object> searchTag(){
        HashMap<String,Object> result = new HashMap<>();
        // 对所有索引进行搜索
        List<String> string = articleService.searchTag();
        result.put("data",string);
        return result;
    }

    /**
     * 文章归档
     */
    @GetMapping(value = "/timeLine")
    public HashMap<String, Object> searchTimeLine(){
        HashMap<String,Object> result = new HashMap<>();
        // 对所有索引进行搜索
        List<TimeLineVo> timeLine = articleService.searchTimeLine();
        result.put("data",timeLine);
        return result;
    }

}
